package com.timeDeal.servlet;



import com.alibaba.druid.util.StringUtils;
import com.timeDeal.bean.JinduBean;
import com.timeDeal.bean.UserBean;
import com.timeDeal.service.JInduServicce;
import com.timeDeal.service.UserService;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2016/7/24.
 */

public class TijiaoJinduServlet extends HttpServlet   {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, IOException {
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setHeaderEncoding("utf-8");
        String banben = request.getParameter("count");
        HashMap<String,String> map = new HashMap<>() ;
        JInduServicce js = new JInduServicce();
        JinduBean jb = new JinduBean();
        UserService us = new UserService() ;
        String  id =request.getParameter("id")  ;
        System.out.println(id);
        String  jindumiaoshu =request.getParameter("jindumiaoshu")  ;
        System.out.println("WWWWWWWWWWWWWWWWWWWW");
        String name =((UserBean)request.getSession().getAttribute("USER")).getYonghuming()+"_"+new Date().getTime();
        try {
            List<FileItem> items = upload.parseRequest(request);
            for (FileItem item : items) {
                if (!item.isFormField()) {
                    String path = "D:\\lianxi";
                    String filename = "";
                    filename = item.getName();//原始文件名
                    name = name+ "." + FilenameUtils.getExtension(filename); //文件重命名
                    System.out.println("upload Path:" + (path + File.separator + name));
                    FileOutputStream os = new FileOutputStream(path + File.separator + name);
                    name = path + File.separator + name ;
                    IOUtils.copy(item.getInputStream(), os);
                    os.flush();
                    os.close();
                } else {

                    System.out.println(item.getFieldName());
                    System.out.println(item.getString());
                    map.put(item.getFieldName(),item.getString()) ;




                }

            }
    } catch (FileUploadException e) {
            e.printStackTrace();
        }
        jb.setShenqingtiqianjieshu(map.get("shifoujingbiao"));
        jb.setMiaoshu(map.get("jindumiaoshu"));
        jb.setXuqiufabuid(Integer.valueOf(map.get("id")));
        jb.setZhanshi(name);
        UserBean ub1 = (UserBean)request.getSession().getAttribute("USER") ;
        jb.setTijiaorenid(Integer.valueOf(us.getDetail(ub1.getZhanghao(),1)[0]));
        jb.setBanben(banben);
         System.out.println(jb.getShenqingtiqianjieshu()+"aaaaaaaaaaaaaa");
        if (js.tijiaoJindu(jb, "xuqiufabu")) {
            PrintWriter out = response.getWriter();
            out.print("<script>alert(\"tijiao sucessful!\");</script>");
            response.setHeader("refresh", "0;url=/TimeDeal/user/zhujiemiangg.jsp");
        } else {
            PrintWriter out = response.getWriter();
            out.print("<script contentType=“text/html;charset=UTF-8”>alert(\"error!\");</script>");
            response.setHeader("refresh", "0;url=/TimeDeal/user/tijiaojindu.jsp");
        }


    }
}